home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / rgrep.zip / RGREP next >
Internet Message Format  |  1991-10-09  |  6KB

  1. Path: tut.cis.ohio-state.edu!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!uunet!mcsun!hp4nl!ruuinf!piet
  2. From: piet@cs.ruu.nl (Piet van Oostrum)
  3. Newsgroups: comp.lang.perl
  4. Subject: Recursive grep in perl
  5. Message-ID: <3985@ruuinf.cs.ruu.nl>
  6. Date: 8 Oct 90 17:03:21 GMT
  7. Sender: news@ruuinf.cs.ruu.nl
  8. Reply-To: piet@cs.ruu.nl (Piet van Oostrum)
  9. Organization: Dept of Computer Science, Utrecht University, The Netherlands
  10. Lines: 191
  11.  
  12. This is my 'recursive grep'. At first sight it is a "find -exec egrep"
  13. replacement. Actually it does things that can't be done with the find/egrep
  14. combination:
  15.   It silently skips binary files (unless told not to)
  16.   It uncompresses compressed file while scanning
  17.   It allows / in filename patters e.g. search in all files src/*.c
  18.   It allows real perl regexps for filename patterns
  19. ------------------------------------------------------------------------
  20. #! /usr/bin/perl
  21.  
  22. die "Usage: rgrep [-iredblL] regexp filepat ...\n       rgrep -h for help\n"
  23.     if $#ARGV < $[;
  24.  
  25. # Written by Piet van Oostrum <piet@cs.ruu.nl>
  26. # This is really free software
  27.  
  28. $nextopt = 1;
  29. $igncase = '';
  30. $regpat = 0;
  31. $links = 0;
  32. $error = 0;
  33. $skipbin = 1;
  34. $debug = 0;
  35.  
  36. do { $regexp = shift (@ARGV); } while &checkopt ($regexp);
  37. $icreg = $igncase;
  38. $igncase = '';
  39.  
  40. eval 'sub grep_file {
  41.         while (<F>) {
  42.         $ln++;
  43.         if (/$regexp/o' . $icreg .') {
  44.             print "$file:$ln:$_";
  45.             print "\n" if substr($_, -1, 1) ne "\n";
  46.         }
  47.         }
  48. }';
  49.  
  50. for (@ARGV) {
  51.     if (! &checkopt ($_)) {
  52.         if ($igncase || $regpat || /[?*[]/ || ! -e) {
  53.         if ($regpat) {
  54.         s/#/\\#/g;
  55.         $_ = "#$_#";
  56.         } else { # translate File pattern into regexp
  57.                 $re = '#($|/)'; $save = $_;
  58.             while (/[[*?+()|.^$#]/) {
  59.             $re .= $`;
  60.             $c = $&;
  61.             $_ = $';
  62.             if ($c eq '*') { $c = '[^/]*'; }
  63.             elsif ($c eq '?') { $c = '[^/]'; }
  64.             elsif ($c eq '[') {
  65.                 if (/.\]/) { $c = "[$`$&"; $_ = $'; }
  66.             else {
  67.                 $error++;
  68.                 printf stderr "Illegal filepattern %s\n", $save;
  69.             }
  70.             } else { $c = "\\$c"; }
  71.             $re .= $c;
  72.         }
  73.         $_ = "$re$_\$#$igncase";
  74.         }
  75.         print "filepat: $_\n" if $debug;
  76.             push (@filepat, $_);
  77.     }
  78.         else { push (@files, $_); print "file: $_\n" if $debug; }
  79.     }
  80. }
  81.  
  82. exit 1 if $errors ;
  83.  
  84. if ($#filepat < $[) {
  85.     eval "sub in_pat {1;}" ;
  86. }
  87. else {
  88.     $subtxt = 'sub in_pat { local ($f) = @_;';
  89.     $or = "";
  90.     for (@filepat) {
  91.     $subtxt .= $or . '$f =~ m' . $_;
  92.     $or = " || ";
  93.     }
  94.     $subtxt .= ';};1';
  95.  
  96.     if (! eval $subtxt) {
  97.         print $@;
  98.         exit 1;
  99.     }
  100.  
  101. @files = (".") if $#files < $[;
  102.  
  103. for $file (@files) {
  104.     &do_grep ($file);
  105. }
  106.  
  107. sub do_grep {
  108.     local ($file) = @_;
  109.     local (*F, $ln, $f, $g, @dirfiles);
  110.     if (-f $file) {
  111.     if (open (F, $file)) {
  112.         if (-B F) { # binary file --  may be compressed/compacted
  113.         if (($cx1 = getc(F)) eq "\377" && (getc(F) eq "\037")) {
  114.             open (F, "uncompact < $file|");
  115.             if ($skipbin && -B F) { close (F); return; }
  116.         }
  117.         elsif ($cx1 eq "\037" && (getc(F) eq "\235")) {
  118.             open (F, "uncompress < $file|");
  119.             if ($skipbin && -B F) { close (F); return; }
  120.         }
  121.         elsif ($skipbin) {
  122.             close (F); return;
  123.         }
  124.         }
  125.         print "Reading $file\n" if $debug;
  126.         &grep_file;
  127.     } else {
  128.         print stderr "Cannot open $file\n";
  129.     }
  130.     }
  131.     elsif (-d $file) {
  132.     print "Entering $file\n" if $debug;
  133.     if (opendir (F, $file)) {
  134.         @dirfiles = readdir (F);
  135.         closedir (F);
  136.         for $f (@dirfiles) {
  137.         next if ($f eq '.' || $f eq '..');
  138.         $g = "$file/$f";
  139.         next if (-l $g && ($links < 1 || $links == 1 && -d $g));
  140.         if (-f $g && &in_pat ($g) || -d _) {
  141.             &do_grep ($g);
  142.         }
  143.         }
  144.     } else {
  145.         print stderr "Can't open $file\n";
  146.     }
  147.     }
  148. }
  149.  
  150. sub checkopt {
  151.     local ($_) = $_[0];
  152.     if (/^-/ && $nextopt) {
  153.         $nextopt = 1;
  154.     @opt = split (/-*/,$_); shift (@opt);
  155.         for $opt (@opt) {
  156.         if ($opt eq 'i') { $igncase = 'i'; }
  157.         elsif ($opt eq 'd') { $debug = 1; }
  158.         elsif ($opt eq 'l') { $links = 1; }
  159.         elsif ($opt eq 'L') { $links = 2; }
  160.         elsif ($opt eq 'b') { $skipbin = 0; }
  161.         elsif ($opt eq 'r') { $regpat = 1; }
  162.         elsif ($opt eq 'e') { $nextopt = 0; }
  163.         elsif ($opt eq 'h' || $opt eq 'H') { & help; }
  164.         else { $error++; printf stderr "Unknown option -%s\n", $opt; }
  165.     }
  166.         return 1;
  167.     }
  168.     $nextopt = 1;
  169.     return 0;
  170. }
  171.  
  172. sub help {
  173.     print <<'HELP'; exit 0;
  174. Usage: rgrep [-iredblL] regexp filepat ...
  175.   regexp = perl regular expression to search
  176.   filepat ... = a list of files and directories to be searched or
  177.       file patterns to match filenames.
  178.       filepat will be interpreted as file or directory name if it exists
  179.       as such, and does not contain the metacharacters [ ] ? or *. After
  180.       the options -i and -r all filepats will be considered patterns.
  181.       rgrep will search all files in any of the directories given (and its
  182.       subdirectories) that match any of the filepats, except binary files.
  183.       Compressed files will be searched in uncompressed form.
  184.       Note: filepats may contain / contrary to find usage.
  185.   -b  Don't skip binary files.
  186.   -i  Ignore case, either in the regexp or in filename matching (depending
  187.       on the location). Before the regexp only applies to the regexp,
  188.       otherwise to the filepats following it.
  189.   -r  The following filepats are treated as real perl regexps rather than
  190.       shell style filename patterns. In this case / is not a special
  191.       character, i.e. it is matched by . and matching is not anchored (you
  192.       must supply ^ and $ yourself). E.g. a.b matches the file /xa/by/zz.
  193.   -l  Do follow symbolic links only for files (default is do not follow).
  194.   -L  Do follow symbolic links for files and directories.
  195.   -e  Do not interpret following argument as option. Useful if regexp or
  196.       filepat starts with a -.
  197.   -d  Debugging: Give a lot of output on what happens.
  198.   -h  print this message and exit.
  199. Piet van Oostrum <piet@cs.ruu.nl>
  200. HELP
  201. }
  202.  
  203.